public class Test2 {
    import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

    public class ProcessCreationUsingProcessBuilder {
        public static void main(String[] args) {
            try {
                // 创建一个 ProcessBuilder 对象，指定要执行的命令
                ProcessBuilder processBuilder = new ProcessBuilder("ls", "-l");
                // 启动进程
                Process process = processBuilder.start();

                // 获取进程的输入流，用于读取进程的输出
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }

                // 等待进程执行完毕并获取返回值
                int exitCode = process.waitFor();
                System.out.println("Process exited with code " + exitCode);
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
